import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/pages/lf_change_password/lf_change_password_logic.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/res/lf_fonts.dart';
import 'package:largefriends/res/lf_string_ext.dart';
import 'package:largefriends/res/lf_text_style.dart';
import 'package:largefriends/res/strings/lf_strings.dart';
import 'package:largefriends/widgets/lf_app_bar.dart';
import 'package:largefriends/widgets/lf_button.dart';
import 'package:largefriends/widgets/lf_text_filed.dart';

import '../../widgets/lf_text_label_filed.dart';
import '../lf_forget_password/lf_forget_password_logic.dart';

class LfChangePasswordPage extends StatefulWidget {
  const LfChangePasswordPage({super.key});

  @override
  State<LfChangePasswordPage> createState() => _LfChangePasswordPageState();
}

class _LfChangePasswordPageState extends State<LfChangePasswordPage> {
  final logic = Get.put(LfChangePasswordLogic());
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: Scaffold(
          appBar: lfAppBar(
            leadingCallback: () {
              Get.back();
            },
            title: strChangePassword.tr,
          ),
          body: Container(
            padding: REdgeInsets.symmetric(horizontal: 16.w),
            height: ScreenUtil().screenHeight,
            child: SingleChildScrollView(
              child: Column(
                children: [
                  20.h.sbh,
              LfTextLabelFiled(
                textEditingController: logic.oldPasswordController,
                isPassword: true,
                hintText: strCurrentPassword.tr,
                prefixIcon: Icon(
                  Icons.lock,
                  color: colorFF007F,
                  size: 20.w,
                ),
                inputFormatters: [
                  LengthLimitingTextInputFormatter(16),
                  FilteringTextInputFormatter.deny(RegExp('[ ]'))
                ],
                hintStyle: lfTextStyle(
                    sp: 14.sp,
                    fontFamily: fontRegular,
                    color: color210215.withAlpha(128)),
                onChanged: (value) {
                  logic.oldPassword.value = value;
                },
              ),
                  3.h.sbh,
                  Align(
                    alignment: Alignment.bottomRight,
                    child: Obx(
                          () {
                            var length = logic.oldPassword.value.trim().length;
                            var tip = "";
                            if(length==0){
                              tip = "";
                            }else if (length< 6) {
                              tip =
                              strCharactersMinimum.tr.replacePercentS(["6"])!;
                            } else {
                              tip = "$length/16";
                            }
                            return Text(
                                tip,
                                style: lfTextStyle(
                                sp: 14.sp,
                                color: color514F62)
                            );
                          },
                      ),
                    ),
                  6.h.sbh,
                  LfTextLabelFiled(
                    textEditingController: logic.newPasswordController,
                    isPassword: true,
                    prefixIcon: Icon(
                      Icons.lock,
                      color: colorFF007F,
                      size: 20.w,
                    ),
                    hintText: strNewPassword.tr,
                    focusNode: logic.newPasswordNode,
                    inputFormatters: [
                      LengthLimitingTextInputFormatter(16),
                      FilteringTextInputFormatter.allow(
                        RegExp('[A-Za-z0-9]'),
                      )
                    ],
                    onChanged: (value) {
                      logic.newPassword.value = value;
                    },
                  ),
                  3.h.sbh,
                  Align(
                    alignment: Alignment.bottomRight,
                    child: Obx(
                          () {
                        var length = logic.newPassword.value.trim().length;
                        var tip = "";
                        if(length==0){
                          tip = "";
                        }else if (length< 6) {
                          tip =
                          strCharactersMinimum.tr.replacePercentS(["6"])!;
                        } else {
                          tip = "$length/16";
                        }
                        return Text(
                            tip,
                            style: lfTextStyle(
                                sp: 14.sp,
                                color: color514F62)
                        );
                      },
                    ),
                  ),
                  6.h.sbh,
                  LfTextLabelFiled(
                    textEditingController: logic.confirmPasswordController,
                    isPassword: true,
                    focusNode: logic.confirmPasswordNode,
                    inputFormatters: [
                      LengthLimitingTextInputFormatter(16),
                      FilteringTextInputFormatter.allow(
                        RegExp('[A-Za-z0-9]'),
                      )
                    ],
                    prefixIcon: Icon(
                      Icons.lock,
                      color: colorFF007F,
                      size: 20.w,
                    ),
                    hintText: strConfirmNewPassword.tr,
                    onChanged: (value) {
                      logic.confirmPassword.value = value;
                    },
                  ),
                  3.h.sbh,
                  Align(
                    alignment: Alignment.bottomRight,
                    child: Obx(
                          () {
                        var length = logic.confirmPassword.value.trim().length;
                        var tip = "";
                        if(length==0){
                          tip = "";
                        }else if (length< 6) {
                          tip =
                          strCharactersMinimum.tr.replacePercentS(["6"])!;
                        } else {
                          tip = "$length/16";
                        }
                        return Text(
                            tip,
                            style: lfTextStyle(
                                sp: 14.sp,
                                color: color514F62)
                        );
                      },
                    ),
                  ),
                  20.h.sbh,
                  Align(
                    alignment: Alignment.topLeft,
                    child: InkWell(
                      child: Text("Forget Password?",
                          style: lfTextStyle(
                            sp: 15.sp,
                            fontFamily: fontBold,
                            color: color514F62,
                            decoration: TextDecoration.underline,
                          )),
                      onTap: () {
                        toForgetPassword(
                            email:
                                logic.authService.currentAccount!.email ?? "");
                      },
                    ),
                  ),
                  80.h.sbh,
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: 20.w),
                    child: Obx(
                      () => LfButton(
                        enabled: logic.enableContinue,
                        text: strContinue.tr,
                        onPress: () {
                          logic.changePassword();
                        },
                      ),
                    ),
                  )
                ],
              ),
            ),
          ),
        ),
        onWillPop: () async {
          Get.back();
          return false;
        });
  }
}
